﻿using System;
using MTC.Common;
using MTC.Common.Enum;
using MTC.Web.Business;
using MTC.Web.Business.DTO;

namespace MTC.Web.Forms
{
    public partial class AccidentEdit : System.Web.UI.Page
    {
        private AccidentEntity _entity;
        Business.Accidents AccidentBO = new Business.Accidents();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the plate id if available
                if (Request.Params[Params.ACCIDENTID] != "0")
                {
                    int accidentid = Convert.ToInt32(Request.Params[Params.ACCIDENTID]);
                    _entity = AccidentBO.GetAnAccident(accidentid);
                    LoadFields();
                }
            }

        }

        private void LoadFields()
        {
            if (_entity == null)
                return;
            hfAccidentId.Value = _entity.AccidentId.ToString();

            cboDriver.SelectedValue = _entity.Name;
            cboVehicle.SelectedValue = _entity.PlateNumber;
            txtCosts.Text = _entity.Costs;
            txtFault.Text = _entity.Fault;
            txtLocation.Text = _entity.Location;
            txtRepairLocation.Text = _entity.RepairedLocation;
            txtComments.Html = _entity.Comments;
            txtDamages.Html = _entity.Damages;
            if (_entity.AccidentDate.HasValue)
                dteAccidentDate.SelectedDate = _entity.AccidentDate.Value;
    

        }

        private void SetupEnvironment()
        {
            cboDriver.DataSource = DropDowns.GetDrivers();
            cboDriver.DataBind();
            cboVehicle.DataSource = DropDowns.GetTaxis();
            cboVehicle.DataBind();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                SetFields();
            }

        }

        private bool SetFields()
        {
            AccidentEntity entity = new AccidentEntity();

            if (hfAccidentId.Value != "")
                entity.AccidentId = Convert.ToInt32(hfAccidentId.Value);

            if (txtComments.Html.Length > 0)
                entity.Comments = txtComments.Html;
            if (txtDamages.Html.Length > 0)
                entity.Damages = txtDamages.Html;
            
            entity.Costs = txtCosts.Text;
            entity.DriverId = Convert.ToInt32(cboDriver.SelectedItem.Value);
            entity.TaxiId = Convert.ToInt32(cboVehicle.SelectedItem.Value);
            entity.Fault = txtFault.Text;
            entity.Location = txtLocation.Text;
            entity.RepairedLocation = txtRepairLocation.Text;
            entity.AccidentDate = dteAccidentDate.SelectedDate;


            if (entity.AccidentId == 0)
            {
                AccidentBO.AddAccident(entity);
            }
            else
            {
                return AccidentBO.UpdateAnAccident(entity);
            }
            return true;
            
        }

        private bool ValidateFields()
        {
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            Response.Redirect(Common.Urls.AccidentListURL(), true);

        }

        private void ClearFields()
        {
            txtCosts.Text = "";
            txtFault.Text = "";
            txtLocation.Text = "";
            txtRepairLocation.Text = "";
            txtComments.Html = "";
            txtDamages.Html = "";
            dteAccidentDate.Clear();

        }

        protected void ASPxPageControl1_ActiveTabChanged(object source, DevExpress.Web.ASPxTabControl.TabControlEventArgs e)
        {

        }
    }
}
